<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item
      ><a href="/">营销</a></el-breadcrumb-item
    >
    <el-breadcrumb-item>添加广告</el-breadcrumb-item>

  </el-breadcrumb>

  <el-divider />


  <el-card class="box-card">  
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="广告名称：" prop="name">
      <el-input v-model="ruleForm.name" />
    </el-form-item>
    <el-form-item label="广告位置：" prop="region">
      <el-select v-model="ruleForm.region" placeholder="App首页轮播">
        <el-option label="APP首页轮播" value="shanghai" />
        <el-option label="Pc首页轮播" value="beijing" />
      </el-select>
    </el-form-item>
   
    <el-form-item label="开始时间：" required>
      <el-col :span="11">
        <el-form-item prop="date1">
          <el-date-picker
            v-model="ruleForm.date1"
            type="date"
            label="Pick a date"
            placeholder="Pick a date"
            style="width: 100%"
          />
        </el-form-item>
        </el-col>
</el-form-item>

 
<el-form-item label="到期时间：" required>
      <el-col :span="11">
        <el-form-item prop="date1">
          <el-date-picker
            v-model="ruleForm.date1"
            type="date"
            label="Pick a date"
            placeholder="Pick a date"
            style="width: 100%"
          />
        </el-form-item>
        </el-col>
</el-form-item>

<el-form-item label="上线/下线：">
      <el-radio-group v-model="form.resource">
        <el-radio label="上线" />
        <el-radio label="下线" />
      </el-radio-group>
    </el-form-item>

    <el-form-item label="广告图片：">
    <el-upload
    ref="uploadRef"
    class="upload-demo"
    action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
    :auto-upload="false"
  >
    <template #trigger>
      <el-button type="primary">添加广告</el-button>
    </template>
<template #tip>
      <div class="el-upload__tip" style="color: darkgrey;">
        只能上传jpg/png文件，且不超过100kb
      </div>
    </template>
  </el-upload>
</el-form-item>

<el-form-item label="排序：" prop="sort">
      <el-input v-model="ruleForm.name" />
    </el-form-item>

    <el-form-item label="广告链接" prop="name">
      <el-input v-model="ruleForm.name" />
    </el-form-item>

    
    
    <el-form-item label="广告备注" prop="desc">
      <el-input v-model="ruleForm.desc" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        提交
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
</el-card>



</template>

<script setup lang="ts">
import { reactive, ref,onMounted } from 'vue'
import { ElMessage, ElMessageBox, FormInstance, FormRules, UploadInstance } from 'element-plus'
import * as api from "../../json/request/api";
import type { IMinioDelete,IAdminMinioUpdate } from "../../json/request/QmsAdminConreiller/index";

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  name: 'Hello',
  region: '',
  count: '',
  date1: '',
  date2: '',
  delivery: false,
  type: [],
  resource: '',
  desc: '',
  
})

const rules = reactive<FormRules>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  region: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change',
    },
  ],
  count: [
    {
      required: true,
      message: 'Please select Activity count',
      trigger: 'change',
    },
  ],
  date1: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change',
    },
  ],
  date2: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a time',
      trigger: 'change',
    },
  ],
  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change',
    },
  ],
  resource: [
    {
      required: true,
      message: 'Please select activity resource',
      trigger: 'change',
    },
  ],
  desc: [
    { required: true, message: 'Please input activity form', trigger: 'blur' },
  ],
})



const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log('submit!')
      ElMessageBox.confirm(
    '是否提交数据?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
      )

        .then(async () => {
      
      ElMessage({
        type: 'success',
        message: '提交成功',
      })
      let res = await api.admin.adminminioupload({
        data: {},
        code: 200,
        message: '提交成功'
      }) 
      console.log(res);
    })
       .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消提交',
      })
    })
      
    } else {
      console.log('error submit!', fields)
    }
  })
  }

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))

const form = reactive({
  resource: '',
})

//上传
const uploadRef = ref<UploadInstance>()

const submitUpload = () => {
  uploadRef.value!.submit()
}


</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.box-card {
  width: 680px;
  margin-left: 200px;
  transform: translateY(-26px);
}
</style>